package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.criteria.MerchantBankCriteria;
import com.ayf.payment.game.api.entity.MerchantBank;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantBankMapper;
import com.ayf.payment.game.api.service.MerchantBankService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.Status;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MerchantBankServiceImpl implements MerchantBankService {

    @Resource
    private MerchantBankMapper merchantBankMapper;

    @Override
    public MerchantBank selectOne(MerchantBank merchantBank) {
        return merchantBankMapper.selectOne(merchantBank);
    }

    @Override
    public MerchantBank getMerchantBankByMid(Integer merchantId) {

        return merchantBankMapper.getMerchantBankByMid(merchantId);
    }

    @Override
    public int insert(MerchantBankCriteria merchantBankCriteria) {
        if (merchantBankCriteria.getMerchantId() == null) {
            throw new BusinessException(CommonConstants.RETURN_ERROR, "商户ID不能为空");
        }
        if (merchantBankCriteria.getSettleType() == null) {
            throw new ParameterException("结算类型不能为空");
        }
        if (StringUtils.isBlank(merchantBankCriteria.getAccountName())) {
            throw new ParameterException("收款人姓名不能为空");
        }
        if (StringUtils.isBlank(merchantBankCriteria.getBankName())) {
            throw new ParameterException("开户银行不能为空");
        }
        if (StringUtils.isBlank(merchantBankCriteria.getBankPhone())) {
            throw new ParameterException("预留手机号不能为空");
        }
        if (StringUtils.isBlank(merchantBankCriteria.getCardNo())) {
            throw new ParameterException("收款账户不能为空");
        }
        MerchantBankCriteria bankCriteria = new MerchantBankCriteria();
        bankCriteria.setMerchantId(merchantBankCriteria.getMerchantId());
        List<MerchantBank> list = selectList(bankCriteria);
        if (list == null || list.size() == 0) {
            merchantBankCriteria.setApplyTime(new Date());
            MerchantBank bank = BeanConvertUtils.convert(merchantBankCriteria, MerchantBank.class);
            bank.setStatus(Status.ENABLE);
            merchantBankMapper.insertSelective(bank);
            return 1;
        }
        MerchantBankCriteria criteria = new MerchantBankCriteria();
        criteria.setMerchantId(merchantBankCriteria.getMerchantId());
        criteria.setStatus(Status.AUDITED);
        List<MerchantBank> banks = merchantBankMapper.selectList(criteria);
        if (banks != null && banks.size() > 0) {
            throw new ParameterException("当前账户已有结算信息正在审核中，请等待审核");
        }
        merchantBankCriteria.setApplyTime(new Date());
        MerchantBank bank = BeanConvertUtils.convert(merchantBankCriteria, MerchantBank.class);
        merchantBankMapper.insertSelective(bank);
        return 2;

    }

    @Override
    public boolean updateBankStatus(MerchantBankCriteria merchantBankCriteria) {
        if (merchantBankCriteria.getOldId() == null) {
            throw new ParameterException("旧银行卡信息ID不能为空");
        }
        if (merchantBankCriteria.getNewId() == null) {
            throw new ParameterException("新银行卡信息ID不能为空");
        }
        if (merchantBankCriteria.getStatus() == null) {
            throw new ParameterException("更改状态不能为空");
        }
        if (merchantBankCriteria.getStatus().equals(Status.ENABLE)) {
            MerchantBank merchantBank = new MerchantBank();
            merchantBank.setStatus(Status.PROHIBIT);
            merchantBank.setId(merchantBankCriteria.getOldId());
            merchantBankMapper.updateByPrimaryKeySelective(merchantBank);
        }
        MerchantBank merchantBank = new MerchantBank();
        merchantBank.setStatus(merchantBankCriteria.getStatus());
        merchantBank.setId(merchantBankCriteria.getNewId());
        if (StringUtils.isNotBlank(merchantBankCriteria.getFailDes())) {
            merchantBank.setFailDes(merchantBankCriteria.getFailDes());
        }
        return merchantBankMapper.updateByPrimaryKeySelective(merchantBank) > 0;
    }

    @Override
    public List<MerchantBank> selectList(MerchantBankCriteria merchantBankCriteria) {
        if (merchantBankCriteria == null) {
            throw new ParameterException("银行卡信息不能为空");
        }
        if (merchantBankCriteria.getMerchantId() == null) {
            throw new ParameterException("商户ID不能为空");
        }
        return merchantBankMapper.selectList(merchantBankCriteria);
    }

    @Override
    public List<MerchantBank> selectAuditedList(MerchantBankCriteria merchantBankCriteria) {
        return merchantBankMapper.selectList(merchantBankCriteria);
    }
}

